package com.example.administrator.nl.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class DialogShareShoppingView extends Dialog implements View.OnClickListener {
    private Context context;//上下文
    private int layoutResID;//布局文件id
    private int[] listenedItem;//监听的控件id


    public DialogShareShoppingView(@NonNull Context context, int layoutResID, int style, int[] listenedItem) {
        super(context, style);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItem = listenedItem;
    }


    public DialogShareShoppingView(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResID);

        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);//设置dialog显示在底部
//        dialogWindow.setWindowAnimations();设置动画效果

        WindowManager windowManager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;// 宽度铺满
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;// 高度自适应
//        dialogWindow.setAttributes(lp);
        dialogWindow.setLayout(lp.width, lp.height);

        setCanceledOnTouchOutside(true);//点击外部Dialog消失
        //遍历控件id添加点击注册
        for (int id : listenedItem) {
            findViewById(id).setOnClickListener(this);
        }
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        getWindow().setAttributes(layoutParams);

        //遍历控件id添加点击注册
        for (int id : listenedItem) {
            findViewById(id).setOnClickListener(this);
        }
    }

    private OnCenterItemClickListener listener;



    public interface OnCenterItemClickListener {
        void OnCenterItemClick(DialogShareShoppingView dialog, View view);
    }  //很明显我们要在这里面写个接口，然后添加一个方法

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }
    @Override
    public void onClick(View v) {
        //注意：我在这里加了这句话，表示只要按任何一个控件的id,弹窗都会消失，不管是确定还是取消。
        listener.OnCenterItemClick(this, v);
    }

}
